package com.skp.shaphan.sql;

public class SQLTokenInfo {
	private boolean tokenFound = false;
	private int startIndex = -1;
	private String token = "";
	private boolean parenSearched = false;
	private boolean parenFound = false;
	private int parenStart;
	private int parenEnd;
	private String parenText;
	private String fullText;

	public boolean isTokenFound() {
		return tokenFound;
	}

	public void setTokenFound(boolean tokenFound) {
		this.tokenFound = tokenFound;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
		if(startIndex < 0) {
			tokenFound = false;
		} else {
			tokenFound = true;
		}
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
		parenSearched = false;
	}
	
	public void check(String token, String sql) {
		this.setToken(token);
		System.out.println("Token = " + token);
		this.setStartIndex(sql.indexOf(token));
		if(startIndex < 0) {
			return;
		}
		if(sql.charAt(startIndex + token.length() - 1) == '(') {
			int endIndex = sql.indexOf(')', startIndex);
			fullText = sql.substring(startIndex, endIndex + 1);
		} else {
			fullText = token;
		}
		System.out.println("full text = '" + fullText + "'");
		System.out.println("  char = '" + sql.charAt(startIndex + token.length() - 1) + "'");
		
	}
	
	public void parenSearch() {
		parenStart = fullText.indexOf('(');
		if(parenStart > 0) {
			parenFound = true;
			parenEnd = fullText.indexOf(')', parenStart);
			if(parenEnd < 0) {
				parenEnd = fullText.length();
			}
		}
		if(parenFound) {
			parenText = fullText.substring(parenStart + 1, parenEnd);
		} else {
			parenText = "";
		}
		parenSearched = true;
	}
	
	public String getParenText() {
		if(!parenSearched) {
			parenSearch();
		}
		return parenText;
	}

	public String getFullText() {
		return fullText;
	}
}
